Skip to content

Commit ce74442

Browse files
committed
Delete threads with no replies
1 parent 599820e commit ce74442

File tree

4 files changed

+47
-19
lines changed

4 files changed

+47
-19
lines changed

kitsune/forums/handlers.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,15 @@ class ThreadListener(UserDeletionListener):
1111
"""Handles thread cleanup when a user is deleted."""
1212

1313
def on_user_deletion(self, user: User) -> None:
14-
15-
sumo_bot = Profile.get_sumo_bot()
16-
Thread.objects.filter(creator=user).update(creator=sumo_bot)
14+
# First get all threads that should be reassigned, then delete the rest
15+
Thread.objects.filter(creator=user, replies__gt=0).update(creator=Profile.get_sumo_bot())
16+
Thread.objects.filter(creator=user).delete()
1717

1818

1919
class PostListener(UserDeletionListener):
2020
"""Handles post cleanup when a user is deleted."""
2121

2222
def on_user_deletion(self, user: User) -> None:
23-
sumo_bot = Profile.get_sumo_bot()
2423
posts = Post.objects.filter(author=user)
2524

2625
post_content_type = ContentType.objects.get_for_model(Post)
@@ -31,10 +30,9 @@ def on_user_deletion(self, user: User) -> None:
3130
).values_list("object_id", flat=True)
3231
)
3332

34-
# delete individual posts b/c of custom delete method
3533
for post in flagged_posts:
3634
post.delete()
3735

3836
Post.objects.filter(author=user).exclude(
3937
id__in=flagged_posts.values_list("id", flat=True)
40-
).update(author=sumo_bot)
38+
).update(author=Profile.get_sumo_bot())

kitsune/forums/tests/test_handlers.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from kitsune.flagit.models import FlaggedObject
44
from kitsune.forums.handlers import PostListener, ThreadListener
5-
from kitsune.forums.models import Post
5+
from kitsune.forums.models import Post, Thread
66
from kitsune.forums.tests import PostFactory, ThreadFactory
77
from kitsune.sumo.tests import TestCase
88
from kitsune.users.models import Profile
@@ -18,8 +18,13 @@ def setUp(self):
1818
def test_multiple_threads_reassignment(self):
1919
"""Test that multiple threads are reassigned correctly."""
2020
thread1 = ThreadFactory(creator=self.user)
21+
PostFactory(thread=thread1)
22+
2123
thread2 = ThreadFactory(creator=self.user)
24+
PostFactory(thread=thread2)
25+
2226
thread3 = ThreadFactory(creator=self.user)
27+
PostFactory(thread=thread3)
2328

2429
self.listener.on_user_deletion(self.user)
2530

@@ -31,7 +36,9 @@ def test_other_users_threads_unaffected(self):
3136
"""Test that other users' threads are not affected."""
3237
other_user = UserFactory()
3338
thread1 = ThreadFactory(creator=self.user)
39+
PostFactory(thread=thread1)
3440
thread2 = ThreadFactory(creator=other_user)
41+
PostFactory(thread=thread2)
3542

3643
self.listener.on_user_deletion(self.user)
3744

@@ -40,6 +47,20 @@ def test_other_users_threads_unaffected(self):
4047
self.assertEqual(thread1.creator.username, self.sumo_bot.username)
4148
self.assertEqual(thread2.creator, other_user)
4249

50+
def test_empty_thread_deletion(self):
51+
"""Test that threads with no replies are deleted."""
52+
empty_thread = ThreadFactory(creator=self.user)
53+
thread_with_replies = ThreadFactory(creator=self.user)
54+
PostFactory(thread=thread_with_replies)
55+
56+
self.listener.on_user_deletion(self.user)
57+
58+
with self.assertRaises(Thread.DoesNotExist):
59+
empty_thread.refresh_from_db()
60+
61+
thread_with_replies.refresh_from_db()
62+
self.assertEqual(thread_with_replies.creator.username, self.sumo_bot.username)
63+
4364

4465
class TestPostListener(TestCase):
4566
def setUp(self):

kitsune/kbforums/handlers.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,12 @@ class ThreadListener(UserDeletionListener):
99
"""Handles thread cleanup when a user is deleted."""
1010

1111
def on_user_deletion(self, user: User) -> None:
12-
13-
sumo_bot = Profile.get_sumo_bot()
14-
Thread.objects.filter(creator=user).update(creator=sumo_bot)
12+
Thread.objects.filter(creator=user, replies__gt=0).update(creator=Profile.get_sumo_bot())
13+
Thread.objects.filter(creator=user).delete()
1514

1615

1716
class PostListener(UserDeletionListener):
1817
"""Handles post cleanup when a user is deleted."""
1918

2019
def on_user_deletion(self, user: User) -> None:
21-
22-
sumo_bot = Profile.get_sumo_bot()
23-
Post.objects.filter(creator=user).update(creator=sumo_bot)
20+
Post.objects.filter(creator=user).update(creator=Profile.get_sumo_bot())

kitsune/kbforums/tests/test_handlers.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from kitsune.kbforums.handlers import PostListener, ThreadListener
2+
from kitsune.kbforums.models import Thread
23
from kitsune.kbforums.tests import PostFactory, ThreadFactory
34
from kitsune.sumo.tests import TestCase
45
from kitsune.users.models import Profile
@@ -13,9 +14,9 @@ def setUp(self):
1314

1415
def test_multiple_threads_reassignment(self):
1516
"""Test that multiple threads are reassigned correctly."""
16-
thread1 = ThreadFactory(creator=self.user)
17-
thread2 = ThreadFactory(creator=self.user)
18-
thread3 = ThreadFactory(creator=self.user)
17+
thread1 = ThreadFactory(creator=self.user, replies=1)
18+
thread2 = ThreadFactory(creator=self.user, replies=1)
19+
thread3 = ThreadFactory(creator=self.user, replies=1)
1920

2021
self.listener.on_user_deletion(self.user)
2122

@@ -26,9 +27,7 @@ def test_multiple_threads_reassignment(self):
2627
def test_other_users_threads_unaffected(self):
2728
"""Test that other users' threads are not affected."""
2829
other_user = UserFactory()
29-
30-
other_user = UserFactory()
31-
thread1 = ThreadFactory(creator=self.user)
30+
thread1 = ThreadFactory(creator=self.user, replies=1)
3231
thread2 = ThreadFactory(creator=other_user)
3332

3433
self.listener.on_user_deletion(self.user)
@@ -38,6 +37,19 @@ def test_other_users_threads_unaffected(self):
3837
self.assertEqual(thread1.creator.username, self.sumo_bot.username)
3938
self.assertEqual(thread2.creator, other_user)
4039

40+
def test_empty_thread_deletion(self):
41+
"""Test that threads with no replies are deleted."""
42+
empty_thread = ThreadFactory(creator=self.user, replies=0)
43+
thread_with_replies = ThreadFactory(creator=self.user, replies=1)
44+
45+
self.listener.on_user_deletion(self.user)
46+
47+
with self.assertRaises(Thread.DoesNotExist):
48+
empty_thread.refresh_from_db()
49+
50+
thread_with_replies.refresh_from_db()
51+
self.assertEqual(thread_with_replies.creator.username, self.sumo_bot.username)
52+
4153

4254
class TestPostListener(TestCase):
4355
def setUp(self):

0 commit comments

Comments
 (0)